//
//  SEGOrderDetailButtonView.m
//  Uhomebk
//
//  Created by mac on 2017/7/10.
//  Copyright © 2017年 com.segi. All rights reserved.
//

#import "SEGOrderDetailButtonView.h"
#import "SEGOrderActionModel.h"
#import "SEGBottomPopController.h"
static  NSString *const kMoreCode = @"Buttom_MoreCode";
@interface SEGOrderDetailButtonView()
{
    NSMutableArray *_moreData;
}

@property(nonatomic,strong)SEGBottomPopController *bottomPopVC_more;/*底部弹出界面 */
@end

@implementation SEGOrderDetailButtonView

- (instancetype)initWithFrame:(CGRect)frame Arr:(NSArray*)arr
{
    if (self == [super initWithFrame:frame]) {
        self=[[SEGOrderDetailButtonView alloc] initWithFrame:frame];
        [self setupUI:arr];
    }
    return self;
}

- (void)setupUI:(NSArray*)arr
{
    int i=0;
    NSMutableArray *bottomData = [[NSMutableArray alloc] init];
    _moreData = [[NSMutableArray alloc] init];
    //大于4个增加一个更多按钮 - 其他的放到更多里面
    if (arr.count > 4){
        for (int i = 0; i < 3; i ++) {
            [bottomData addObject:arr[i]];
        }
        SEGOrderActionModel *actionModel = [SEGOrderActionModel initWithDictionary:@{}];
        actionModel.actionName = @"更多";
        actionModel.actionCode = kMoreCode;
        [bottomData addObject:actionModel];
        for (int j = 3; j< arr.count; j ++) {
            [_moreData addObject:arr[j]];
        }
        [self.bottomPopVC_more configDataArrayWithDataArray:_moreData popVCType:SEGBottomPopControllerType_MORE_ACTION];
    }
    else{
        bottomData = (NSMutableArray *)arr;
    }
    
    CGFloat btnWidth=LYScreenWidth()/bottomData.count;
    for (SEGOrderActionModel *actionModel in bottomData) {
        CustomButtonView *abutton = [self creatButton];
        abutton.value = actionModel.actionCode;
        abutton.frame=CGRectMake(i*btnWidth, 0, btnWidth, LYSafeAreaBottom()+44);
        abutton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, LYSafeAreaBottom(), 0);

        NSAttributedString *titleStr = [SEGUtils getAttributeStringWith:kColor_W font:kFont_16 textStr:actionModel.actionName lineSpace:3 alignment:NSTextAlignmentCenter];
        abutton.titleLabel.numberOfLines = 2;
        [abutton setAttributedTitle:titleStr forState:UIControlStateNormal];
        abutton.tempData = actionModel ;
        [self addSubview:abutton];

        if(i!=bottomData.count-1){
                UIView *line = [[UIView alloc]initWithFrame:CGRectMake(btnWidth-0.5, 6, 0.5, 32)];
                line.backgroundColor = kColor_W;
                [abutton addSubview:line];
            }
            i++;
        }
}

-(CustomButtonView*)creatButton
{
    CustomButtonView *button=[CustomButtonView buttonWithType:UIButtonTypeCustom];
    [button setTitleColor:kColor_W forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageWithColor:kColor_T] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageWithColor:kColor_T_Press] forState:UIControlStateHighlighted];
    button.titleLabel.font = kFont_16;
    [button addTarget:self action:@selector(selectBtn:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

-(void)selectBtn:(CustomButtonView*)sender
{
//    SEGOrderActionModel *actionModel = sender.tempData;
    //点击了更多
//    if ([actionModel.actionCode isEqualToString:kMoreCode]) {
//        [self.bottomPopVC_more segBottomPopControllerShowWith:SEGBottomPopControllerType_MORE_ACTION cellClick:^(SEGOrderActionModel *actionModel) {
//            //点击cell回调
//            if (self.delegate && [self.delegate respondsToSelector:@selector(clickBottomButtonAction:)]) {
//                [self.delegate clickBottomButtonAction:actionModel];
//            }
//        }];
//    }
//    //其他的按钮直接回调
//    else{
        if (self.delegate && [self.delegate respondsToSelector:@selector(clickBottomButtonAction:)]) {
            [self.delegate clickBottomButtonAction:sender.tempData];
        }
//    }
}

-(SEGBottomPopController *)bottomPopVC_more
{
    if (!_bottomPopVC_more) {
        _bottomPopVC_more = [[SEGBottomPopController alloc] init];
    }
    
    return _bottomPopVC_more;
}
@end
